﻿package Server.AHMJ.LAHZ;


import java.util.Random;

class MajPaiQiang {
	public int CountArray(int by[]) {
		return by.length;
	}

	public int sizeof(int by[]) {
		return by.length;
	}

	public void CopyMemory(int[] by, int[] by2, int length) {
		for (int i = 0; i < length; i++) {
			by[i] = by2[i];
		}
	}

	public void memcpy(int[] by, int[] by2, int length) {
		for (int i = 0; i < length; i++) {
			by[i] = by2[i];
		}
	}

	public Random rd = new Random();

	public int rand() {
		return rd.nextInt(1000);
	}

	int m_byFetchPos;
	int m_byMjPai[] = new int[Constants.MAX_CARD_NUM_112];
	int m_cbCardDataArray[] = {
			1, 2, 3, 4, 5, 6, 7, 8,9,// 0-8 一万-九万
			11, 12, 13, 14, 15, 16, 17,18,19, // 9-17 一筒-九筒
		    21, 22, 23, 24, 25, 26,27,28,29, // 18-26 一条-九条
			35,

		    1, 2, 3, 4, 5, 6, 7, 8,9,// 0-8 一万-九万
			11, 12, 13, 14, 15, 16, 17,18,19, // 9-17 一筒-九筒
		    21, 22, 23, 24, 25, 26,27,28,29, // 18-26 一条-九条
		    35,

		    1, 2, 3, 4, 5, 6, 7, 8,9,// 0-8 一万-九万
			11, 12, 13, 14, 15, 16, 17,18,19, // 9-17 一筒-九筒
		    21, 22, 23, 24, 25, 26,27,28,29, // 18-26 一条-九条
		    35,

		    1, 2, 3, 4, 5, 6, 7, 8,9,// 0-8 一万-九万
			11, 12, 13, 14, 15, 16, 17,18,19, // 9-17 一筒-九筒
		    21, 22, 23, 24, 25, 26,27,28,29, // 18-26 一条-九条
		    35,
	};

	MajPaiQiang() {
		m_byFetchPos = 0;
		memcpy(m_byMjPai, m_cbCardDataArray, sizeof(m_byMjPai));
	}

	void Ruffle(int in_RandCount) {

		m_byFetchPos = 0;
		memcpy(m_byMjPai, m_cbCardDataArray, sizeof(m_byMjPai));

		int randseed1 = 0;
		int randseed2 = 0;
		for (int i = 0; i < in_RandCount; i++) {
			randseed1 += randseed2;
			randseed2 = Constants.rand();
			int randPos1 = randseed1 % Constants.MAX_CARD_NUM_112;
			int randPos2 = randseed2 % Constants.MAX_CARD_NUM_112;

			int by1, by2;
			by1 = m_byMjPai[randPos1];
			by2 = m_byMjPai[randPos2];
			m_byMjPai[randPos1] = by2;
			m_byMjPai[randPos2] = by1;
		}
	}

	int GetZdPai(int seat,int pai){
		return pai;
	}
	int GetPai() // 获取当前麻将
	{
	       //获取全部的牌                       
		int byPai = m_byMjPai[m_byFetchPos];
        //判断 如果牌是万筒条，并且牌不是无效的
		if (MajHelper.IsWTSZ(byPai) && byPai != Constants.INVALID_CARD) {
			m_byMjPai[m_byFetchPos] = Constants.INVALID_CARD;
			//如果牌的无效的，指针往后移动一次
			m_byFetchPos++;
			return byPai;
		}

		return Constants.INVALID_CARD;
	}
	int GetGuiPai()
	{
		
		int idx = m_byMjPai.length - 8;
		int byPai = m_byMjPai[idx];

		if (MajHelper.IsWTSZ(byPai)&& byPai != Constants.INVALID_CARD) {
		
			return byPai;
		}
		
		return Constants.INVALID_CARD;
	}
	public void getMaPai(int maPaiCount,int[] maPai){
		
		int[] maPaiArray = new int[112];
		int yuPaiCount = 0;
		int idx = -1;
		for(int i=0;i<Constants.MAX_CARD_NUM_112;i++){
			
			if(m_byMjPai[i] != Constants.INVALID_CARD){
				yuPaiCount ++;
				idx ++;
				maPaiArray[idx] = m_byMjPai[i];
			}
		}
		//如果翻牌数量大于余牌数量就取余牌的数量，否则就去余牌
		maPaiCount = maPaiCount>yuPaiCount?yuPaiCount:maPaiCount;
		
		
		
		for(int i =0;i<maPaiCount;i++){
			maPai[i] = maPaiArray[i];
			
		}
		
	}
	int GetGangMoPai() // 获取杠牌的摸牌
	{
		return GetPai();
	}

	int GetBuHuaMoPai() // 获取补花的摸牌
	{
		return GetPai();
	}

	int GetFetchPosition() {
		return m_byFetchPos;
	}

	int IsEnd() {
		if (m_byFetchPos >= Constants.MAX_CARD_NUM_112) {
			return 1;
		}
		return 0;
	}

	public static int PaiWei = 34;

	int GetYuPaiCount() {
		return (Constants.MAX_CARD_NUM_112 - m_byFetchPos);
	}

	static int rand_inc = 0;

	int GetHuanPai(int byMj)// 开始时的换牌,随机从最后的地方开始取
	{
		rand_inc++;

		int randseed = rand() + rand_inc;

		int pos = 1;
		if (Constants.MAX_CARD_NUM_112 - m_byFetchPos != 0) {
			pos = randseed % (Constants.MAX_CARD_NUM_112 - m_byFetchPos);
		}
		if (pos == 0) {
			pos = 1;
		}
		int byHuanMj = m_byMjPai[Constants.MAX_CARD_NUM_112 - pos];
		if (byHuanMj == byMj) {
			randseed = rand() + rand_inc;
			if (Constants.MAX_CARD_NUM_112 - m_byFetchPos != 0) {
				pos = randseed % (Constants.MAX_CARD_NUM_112 - m_byFetchPos);
			}
			if (pos == 0) {
				pos = 1;
			}
			byHuanMj = m_byMjPai[Constants.MAX_CARD_NUM_112 - pos];
		}
		if (byHuanMj == byMj) {
			randseed = rand() + rand_inc;
			if (Constants.MAX_CARD_NUM_112 - m_byFetchPos != 0) {
				pos = randseed % (Constants.MAX_CARD_NUM_112 - m_byFetchPos);
			}
			if (pos == 0) {
				pos = 1;
			}
			byHuanMj = m_byMjPai[Constants.MAX_CARD_NUM_112 - pos];
		}
		m_byMjPai[Constants.MAX_CARD_NUM_112 - pos] = byMj;
		return byHuanMj;
	}
}

class S_PaiJu {

	int byFaPaiSeZi[] = new int[3];
	MajSide MajSides[] = new MajSide[4]; // 摸风后的座位号，东南西北
	int byLastUser; // 上轮出牌玩家
	int byLastOutMj; // 上次出牌
	int byCurrentUser; // 当前玩家
	int byZhuang; // 谁是庄
	int byLianZhuang; // 连庄数
	int byQuanFeng; // 当前局的风位

	void Init() {
		for (int i = 0; i < MajSides.length; i++) {
			MajSides[i] = new MajSide();
		}
		MajSides[0].Init();
		MajSides[1].Init();
		MajSides[2].Init();
		MajSides[3].Init();
		byLastUser = 0;
		byCurrentUser = 0;
		byZhuang = 0;
		byLianZhuang = 0;
		byQuanFeng = 0;
	}
}

class S_JieSuan_Info {
	int lRoundScore;
	int bZiMo; // 每次胡是否自摸

	void Init() {
		bZiMo = 0;
		lRoundScore = 0;
	}
};

class MajLogic {
	int m_byOutMj[] = new int[Constants.MAX_CARD_NUM_112];// 打出去的桌面显示的牌
	int m_byOutMjCount; // 打出了多少张牌
	long m_wRound; // 总局数，只是为了记录日志用
	long m_wMaxRound; // 总局数，只是为了记录日志用
	int m_ActionRight[] = new int[4]; // 操作权限,6-胡,4-杠,3-碰,
	int m_byCurrentAction;
	S_PaiJu m_kPaiJu = new S_PaiJu();

	{
		m_kPaiJu.Init();
	}

	S_JieSuan_Info m_kJieSuan[] = new S_JieSuan_Info[4];

	{
		for (int i = 0; i < m_kJieSuan.length; i++) {
			m_kJieSuan[i] = new S_JieSuan_Info();
		}
	}

	MajLogic() {
		Init();
	}

	private void Init() {
		Init(Constants.INIT_TOTAL);
	}

	void Init(int byReason) {
		m_kPaiJu.byLastUser = Constants.INVALID_SEAT_VALUE;
		m_kPaiJu.MajSides[0].Init();
		m_kPaiJu.MajSides[1].Init();
		m_kPaiJu.MajSides[2].Init();
		m_kPaiJu.MajSides[3].Init();
		m_kPaiJu.byLastOutMj = Constants.INVALID_CARD;
		m_ActionRight[0] = 0;
		m_ActionRight[1] = 0;
		m_ActionRight[2] = 0;
		m_ActionRight[3] = 0;
		m_byCurrentAction = 0;
		if (Constants.INIT_TOTAL == byReason) {
			m_kPaiJu.byZhuang = 0;
			m_kPaiJu.byLianZhuang = 0;
			m_kPaiJu.byQuanFeng = 0;
			m_wRound = 0L;
			m_wMaxRound = 0L;
		}

		if (Constants.INIT_FIRST_ROUND == byReason) {
			m_kPaiJu.byZhuang = 0;
			m_kPaiJu.byLianZhuang = 0;
			m_kPaiJu.byQuanFeng = 0;
			m_wRound = 0L;
			m_wMaxRound = 0L;
		}
		if (Constants.INIT_NEWROUND == byReason) {
		}
		m_kPaiJu.byCurrentUser = m_kPaiJu.byZhuang;
		m_kJieSuan[0].Init();
		m_kJieSuan[1].Init();
		m_kJieSuan[2].Init();
		m_kJieSuan[3].Init();
		memset(m_byOutMj, Constants.INVALID_CARD, Constants.MAX_CARD_NUM_112);
		m_byOutMjCount = 0;
	}

	/*
	 * void SerializePaiJu(S_PaiJu &kPaiJu) { int byFaPaiSeZi[3]; MajSide
	 * MajSides[4]; //摸风后的座位号，东南西北 int byMjPaiQiang[4][36]; int byLastUser;
	 * //上轮出牌玩家 int byCurrentUser; //当前玩家 int byZhuang; //谁是庄 int byLianZhuang;
	 * //连庄数 int byQuanFeng; //当前局的风位 int byMoPai; // int byChuPai; // }
	 */

	private void memset(int[] m_byOutMj2, int value, int length) {
		for (int i = 0; i < m_byOutMj2.length; i++) {
			m_byOutMj2[i] = value;
		}

	}

	boolean IsValidSeatID(int bySeatID) {
		if (bySeatID < 4) {
			return true;
		}
		return false;
	}

	boolean IsNeedMoFeng(int in_Quan) {
		if (IsValidSeatID(m_kPaiJu.byZhuang) == false || GetRound() == 0) {
			return true;
		}
		return false;
	}

	void MoFeng(int byFengValue[]) // 摸风
	{

		int temp = rand() % 24;

		int kFengValues[][] = { { 29, 27, 28, 30 }, { 29, 28, 27, 30 }, { 30, 28, 29, 27 }, { 30, 27, 28, 29 },
				{ 28, 29, 30, 27 }, { 27, 28, 30, 29 }, { 29, 30, 28, 27 }, { 27, 30, 28, 29 }, { 30, 29, 28, 27 },
				{ 30, 27, 29, 28 }, { 30, 28, 27, 29 }, { 29, 28, 30, 27 }, { 27, 29, 28, 30 }, { 27, 29, 30, 28 },
				{ 27, 30, 29, 28 }, { 29, 27, 30, 28 }, { 28, 30, 29, 27 }, { 30, 29, 27, 28 }, { 27, 28, 29, 30 },
				{ 29, 30, 27, 28 }, { 28, 27, 29, 30 }, { 28, 27, 30, 29 }, { 28, 30, 27, 29 }, { 28, 29, 27, 30 } };
		memcpy(byFengValue, kFengValues[temp], 4);
	}

	private int rand() {
		return new Random().nextInt(1024);
	}

	private void memcpy(int[] byFengValue, int[] bytes, int length) {
		for (int i = 0; i < length; i++) {
			byFengValue[i] = bytes[i];
		}
	}

	void SetCurrentUser(int byCurrentUser) {
		m_kPaiJu.byCurrentUser = byCurrentUser;
	}

	void SetLastUser(int byLastUser) {
		m_kPaiJu.byLastUser = byLastUser;
	}

	void SetLastOutMj(int byLastOutMj) {
		m_kPaiJu.byLastOutMj = byLastOutMj;
	}

	void SetZhuang(int byZhuang) {
		m_kPaiJu.byZhuang = byZhuang;
	}

	void SetLianZhuang(int byLianZhuang) {
		m_kPaiJu.byLianZhuang = byLianZhuang;
	}

	void SetQuanFeng(int byQuanFeng) {
		m_kPaiJu.byQuanFeng = byQuanFeng;
	}

	void SetRound(long wRound) {
		m_wRound = wRound;
	}

	void SetMaxRound(long wRound) {
		m_wMaxRound = wRound;
	}

	void SetCurrentAction(int byAction) {
		m_byCurrentAction = byAction;
	}

	int GetCurrentAction() {
		return m_byCurrentAction;
	}

	int GetCurrentUser() {
		if (IsValidSeatID(m_kPaiJu.byCurrentUser) == false) {

			return 0;
		}
		return m_kPaiJu.byCurrentUser;
	}

	int GetLastUser() {
		if (IsValidSeatID(m_kPaiJu.byLastUser) == false) {
			return 0;
		}
		return m_kPaiJu.byLastUser;
	}

	int GetLastOutMj() {
		if (MajHelper.IsWTSZ(m_kPaiJu.byLastOutMj) == false) {
			return Constants.INVALID_CARD;
		}
		return m_kPaiJu.byLastOutMj;
	}

	int GetZhuang() {
		if (IsValidSeatID(m_kPaiJu.byZhuang) == false) {
		}
		return m_kPaiJu.byZhuang;
	}

	int GetLianZhuang() {
		return m_kPaiJu.byLianZhuang;
	}

	int GetQuanFeng() {
		return m_kPaiJu.byQuanFeng;
	}

	long GetRound() {
		return m_wRound;
	}

	long GetMaxRound() {
		return m_wMaxRound;
	}

	public MajSide GetMajSide(int bySeatID) {
		return m_kPaiJu.MajSides[bySeatID];
	}
}

class S_MoFeng {
	int byMoFengSeZi[] = new int[3];
	int byFeng[] = new int[4];
	long dwSeatUserID[] = new long[4];

	void Init() {
	}
};
